﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

#region Disease
namespace backoffice.Data
{
    partial class Disease
    {
        public void setDisease(Disease d)
        {
            this.DiseaseID = d.DiseaseID;
            this.Name = d.Name;
            this.Description = d.Description;
        }
    }
}
#endregion

#region dataDisease
namespace backoffice.Data
{
    public class dataDisease
    {
        #region Create
        public static void createDisease(Disease d)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                d.DiseaseID = Guid.NewGuid();
                
                db.Diseases.InsertOnSubmit(d);
                db.SubmitChanges();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Get
        public static Disease getDisease(Guid guid)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Diseases.FirstOrDefault(d => d.DiseaseID == guid);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Disease> getDiseases(Expression<Func<Disease, bool>> whereClause)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Diseases.Where(whereClause);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Plants_Disease> getDiseases_Plant(Expression<Func<Plants_Disease, bool>> whereClause)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Plants_Diseases.Where(whereClause);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Set
        public static Boolean changeDisease(Disease d)
        {
            try
            {
                if (d == null) return false;
                else
                {
                    db2growDataContext db = new db2growDataContext();

                    Disease res = db.Diseases.FirstOrDefault(dis => dis.DiseaseID == d.DiseaseID);

                    if (res == null) return false;
                    else
                    {
                        res.setDisease(d);

                        db.SubmitChanges();
                        
                        return true;
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Delete
        public static Boolean deleteDisease(Disease d)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                if (db.Diseases.Contains(d))
                {
                    Disease res = db.Diseases.FirstOrDefault(di => di.DiseaseID == d.DiseaseID);

                    if (res == null) return false;
                    else
                    {
                        // Delete plant_disease
                        IQueryable<Plants_Disease> pd = db.Plants_Diseases.Where((Plants_Disease p_d) => p_d.DiseaseID == res.DiseaseID);
                        db.Plants_Diseases.DeleteAllOnSubmit(pd);
                        
                        // Delete disease
                        db.Diseases.DeleteOnSubmit(res);
                        db.SubmitChanges();
                        return true;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion
    }
}
#endregion